//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.Samples.ServiceBus.SilverlightChat.Models.Commands
{
    using System;
    using System.Windows.Input;
    using Microsoft.Samples.ServiceBus.Messaging;
    using Microsoft.Samples.ServiceBus.SilverlightChat.Models.ViewModels;

    /// <summary>
    /// Encapsulates a command to delete a topic from the service namespace
    /// </summary>
    public class DeleteTopicCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            // The CommandParameter for this command is bound to the View Model of the topic we
            // need to delete
            var viewModel = (ChatTopicViewModel)parameter;

            // Asynchronously begin to delete the topic - store the view model of the topic in the 
            // AsyncState so that it can be retrieved and update in the callback (OnDeleteTopicCompleted)
            TopicClient.BeginDeleteTopic(viewModel.Name, App.Context.TokenProvider, this.OnDeleteTopicCompleted, viewModel);
        }

        protected void OnCanExecuteChanged(object sender, EventArgs e)
        {
            if (this.CanExecuteChanged != null)
            {
                this.CanExecuteChanged(sender, e);
            }
        }

        private void OnDeleteTopicCompleted(IAsyncResult result)
        {
            // Get the view model for the topic that was being deleted
            var viewModel = (ChatTopicViewModel)result.AsyncState;

            try
            {
                // Call end topic to end the async operation - if the operation failed, an exception
                // will be thrown here
                TopicClient.EndDeleteTopic(result);

                // Update the UI to remove this topic from the view
                viewModel.Dispatcher.BeginInvoke(() =>
                {
                    var mainPage = (MainPage)App.Current.RootVisual;
                    var chatWindowViewModel = (ChatWindowViewModel)mainPage.chatWindow.DataContext;

                    chatWindowViewModel.Topics.RemoveTopics(viewModel.TopicClient);
                });
            }
            catch (Exception e)
            {
                App.ShowError(e);
            }
        }
    }
}
